const app = getApp();
Page({
  data: {
    typeIndex: 1,
    type:['增值税普通发票','增值税专用发票'],
    info: {},
  },
  onLoad: function (options) {
    let that = this;
    let invoices_id = options.id || '';
    let typeIndex = options.type || 1;
    app.request('invoices/detail', { invoices_id },'GET',false,function(res){
      that.setData({
        pageShow: true,
        options: options,
        typeIndex: typeIndex,
        info: res.data.data || {},
      })
    })
  },
  getFormid(e){
    app.getFormid(e);
  },
  typeChoose(e){
    let that = this;
    let index = e.currentTarget.dataset.index;
    that.setData({
      typeIndex: index
    })
  },
  getInput(e){
    let that = this;
    let info = that.data.info;
    let type = e.currentTarget.dataset.type;
    info[type] = e.detail.value;
    that.setData({
      info: info,
    })
  },
  sure(e){
    let that = this;
    let info = that.data.info;
    let id = that.data.options.id;
    let type = that.data.typeIndex;
    let invoice  = 2;
    let name = info.name;
    let number = info.number;
    let bank = info.bank;
    let bank_no = info.bank_no;
    let address = info.address;
    let phone = info.phone;
    if(!name){
      wx.showToast({
        title: '名称不能为空',
        icon: 'none',
      })
      return false;
    }
    if(!number){
      wx.showToast({
        title: '税号不能为空',
        icon: 'none',
      })
      return false;
    }
    if (!bank && type == 2) {
      wx.showToast({
        title: '开户行不能为空',
        icon: 'none',
      })
      return false;
    }
    if (!bank_no && type == 2) {
      wx.showToast({
        title: '开户行不能为空',
        icon: 'none',
      })
      return false;
    }
    if (!address && type == 2) {
      wx.showToast({
        title: '开户行不能为空',
        icon: 'none',
      })
      return false;
    }
    if (!phone && type == 2) {
      wx.showToast({
        title: '开户行不能为空',
        icon: 'none',
      })
      return false;
    }
    app.request('invoices/addInvoices',{
      id, type, invoice, name, number, bank, bank_no, address, phone
    },'GET',false,function(res){
      wx.showToast({
        title: '保存成功',
      })
      setTimeout(function () {
        wx.navigateBack({
          delta: 1,
        })
      }, 2000);
    })
  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {
    return {
      path: '/pages/index/index'
    }
  }
})